﻿using System;
using System.Runtime.Serialization;
using UFIDA.U9.WSS.ProxyService.Models;
using UFIDA.U9.WSS.ProxyService.Utils;
using UFSoft.UBF.Util.Log;

namespace UFIDA.U9.WSS.ProxyService.Token
{
    [DataContract]
    public class TokenData
    {
        private static readonly ILogger Logger = LoggerManager.GetLogger("TokenData");
        private string _culture = string.Empty;
        private string _supportCultureNameList = string.Empty;

        /// <summary>
        ///     构造器
        /// </summary>
        public TokenData()
        {
            TokenID = Guid.NewGuid().ToString();
        }

        /// <summary>
        ///     构造器
        /// </summary>
        /// <param name="contextInfo"></param>
        /// <param name="invalidTime"></param>
        public TokenData(ContextInfo contextInfo, DateTime invalidTime) : this()
        {
            EnterpriseID = contextInfo.EnterpriseID;
            EnterpriseName = contextInfo.EnterpriseName;
            OrgID = contextInfo.OrgID;
            OrgCode = contextInfo.OrgCode;
            OrgName = contextInfo.OrgName;
            UserID = contextInfo.UserID;
            UserCode = contextInfo.UserCode;
            UserName = contextInfo.UserName;
            Culture = contextInfo.Culture;
            SupportCultureNameList = contextInfo.SupportCultureNameList;
            InvalidTime = invalidTime;
        }

        /// <summary>
        ///     TokenID
        /// </summary>
        [DataMember]
        public string TokenID { get; private set; }

        /// <summary>
        ///     企业ID
        /// </summary>
        [DataMember]
        public string EnterpriseID { get; set; }

        /// <summary>
        ///     企业名称
        /// </summary>
        [DataMember]
        public string EnterpriseName { get; set; }

        /// <summary>
        ///     组织ID
        /// </summary>
        [DataMember]
        public string OrgID { get; set; }

        /// <summary>
        ///     组织编码
        /// </summary>
        [DataMember]
        public string OrgCode { get; set; }

        /// <summary>
        ///     组织名称
        /// </summary>
        [DataMember]
        public string OrgName { get; set; }

        /// <summary>
        ///     用户ID
        /// </summary>
        [DataMember]
        public string UserID { get; set; }

        /// <summary>
        ///     用户编码
        /// </summary>
        [DataMember]
        public string UserCode { get; set; }

        /// <summary>
        ///     用户名称
        /// </summary>
        [DataMember]
        public string UserName { get; set; }

        /// <summary>
        ///     语言
        /// </summary>
        [DataMember]
        public string Culture
        {
            get { return string.IsNullOrEmpty(_culture) ? Constant.DefaultContextCulture : _culture; }
            set { _culture = value; }
        }

        /// <summary>
        ///     支持语言列表，多个中间用,分隔
        /// </summary>
        [DataMember]
        public string SupportCultureNameList
        {
            get { return string.IsNullOrEmpty(_supportCultureNameList) ? Culture : _supportCultureNameList; }
            set { _supportCultureNameList = value; }
        }

        /// <summary>
        ///     失效时间
        /// </summary>
        [DataMember]
        public DateTime InvalidTime { get; set; }

        /// <summary>
        ///     转换为ContextInfo
        /// </summary>
        /// <returns></returns>
        public ContextInfo TransContextInfo()
        {
            ContextInfo contextInfo = new ContextInfo();
            contextInfo.EnterpriseID = EnterpriseID;
            contextInfo.EnterpriseName = EnterpriseName;
            contextInfo.OrgID = OrgID;
            contextInfo.OrgCode = OrgCode;
            contextInfo.OrgName = OrgName;
            contextInfo.UserID = UserID;
            contextInfo.UserCode = UserCode;
            contextInfo.UserName = UserName;
            contextInfo.Culture = Culture;
            contextInfo.SupportCultureNameList = SupportCultureNameList;
            return contextInfo;
        }
    }
}